package com.samsungchaton.common.exception;

import com.samsungchaton.common.constants.HttpStatus;
import com.samsungchaton.common.faultcode.message.ErrorMessage;

/**
 * CommonServiceException
 * 
 * @author sr80.choi
 */
public class CommonServiceException extends RuntimeException {

	private static final long serialVersionUID = -8790228028147064874L;

	protected HttpStatus statusCode;

	private ErrorMessage errorMessage;

	public CommonServiceException(HttpStatus statusCode) {
		this(statusCode, null);
	}

	public CommonServiceException(Throwable t, HttpStatus statusCode) {
		this(t, statusCode, null);
	}

	public CommonServiceException(HttpStatus statusCode, ErrorMessage errorMessage) {
		this(null, statusCode, errorMessage);
	}

	public CommonServiceException(Throwable t, HttpStatus statusCode, ErrorMessage errorMessage) {
		super(t);
		this.statusCode = statusCode;
		this.errorMessage = errorMessage;
	}

	public final Integer getHttpResponseCode() {
		return statusCode.getStatusCode();
	}

	public final String getErrorMessage() {
		String msg = null;

		if (errorMessage != null) {
			msg = errorMessage.getMessage();
		}

		return msg;
	}
	
	public final String getErrorCode() {
		String msg = null;

		if (errorMessage != null) {
			msg = errorMessage.getFaultCode();
		}

		return msg;
	}

	@Override
	public String getMessage() {
		return getErrorMessage();
	}

	@Override
	public String getLocalizedMessage() {
		return getErrorMessage();
	}
}
